package com.draemoncode.weixindb.mvc;

import com.alibaba.druid.util.StringUtils;
import com.draemoncode.weixindb.constant.ValueConstant;
import com.draemoncode.weixindb.conventer.BusiConventer;
import com.draemoncode.weixindb.domain.*;
import com.draemoncode.weixindb.mvc.easyuivo.DataListVO;
import com.draemoncode.weixindb.mvc.exception.BusiException;
import com.draemoncode.weixindb.mvc.vo.MessageTemplateVO;
import com.draemoncode.weixindb.mvc.vo.ShangZhuangVO;
import com.draemoncode.weixindb.repository.*;
import com.draemoncode.weixindb.service.BusiService;
import com.draemoncode.weixindb.util.ValueUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class QueryController {

    @Autowired
    private B_MemberRepository b_memberRepository;

    @Autowired
    private BusiService busiService;

    @Autowired
    private B_PodRepository b_podRepository;

    @Autowired
    private B_XiaZhuRepository b_xiaZhuRepository;

    @Autowired
    private B_HongBaoRepository b_hongBaoRepository;

    @Autowired
    private B_AutoMessage_TemplateRepository b_autoMessage_templateRepository;

    @Autowired
    private B_Calculate_CodeRepository b_calculate_codeRepository;

    @Autowired
    private B_Calculate_EnvRepository b_calculate_envRepository;

    @RequestMapping(value = "/qun_members", method = RequestMethod.GET)
    public DataListVO qun_members(@RequestParam(value = "name", required = false) String name){
        DataListVO dataListVO = new DataListVO();
        PageRequest pageRequest = new PageRequest(0, 1000);
        Page<B_Member> result = null;
        if(!StringUtils.isEmpty(name)){
            result = b_memberRepository.findByNameLike(name, pageRequest);
        }else{
            result = b_memberRepository.findAll(pageRequest);
        }
        dataListVO.setRows(result.getContent());
        dataListVO.setTotal((int) result.getTotalElements());
        return dataListVO;
    }

    @RequestMapping(value = "/members", method = RequestMethod.GET)
    public DataListVO members(@RequestParam(value = "name", required = false) String name, @RequestParam("page") int page,  @RequestParam("rows") int size){
        DataListVO dataListVO = new DataListVO();
        PageRequest pageRequest = new PageRequest(page - 1, size);
        Page<B_Member> result = null;
        if(!StringUtils.isEmpty(name)){
            result = b_memberRepository.findByNameLike(name, pageRequest);
        }else{
            result = b_memberRepository.findAll(pageRequest);
        }
        dataListVO.setRows(result.getContent());
        dataListVO.setTotal((int) result.getTotalElements());
        return dataListVO;
    }

    @RequestMapping(value = "/pod/xiazhu", method = RequestMethod.GET)
    public DataListVO pod_xiazhu(@RequestParam(value = "pod_id", required = false) Integer podId){
        DataListVO dataListVO = new DataListVO();
        Integer newPodId = mgPodId(podId);
        if(newPodId != null){
            List<B_XiaZhu> b_xiaZhuList = b_xiaZhuRepository.findByPod_id(newPodId);
            dataListVO.setRows(b_xiaZhuList);
            dataListVO.setTotal(b_xiaZhuList.size());
        }
        return dataListVO;
    }

    @RequestMapping(value = "/caculatecode/list", method = RequestMethod.GET)
    public DataListVO caculateCodeList(@RequestParam(value = "name", required = false) String name){
        DataListVO dataListVO = new DataListVO();
        List<B_Calculate_Code> b_calculate_codeList = null;
        if(StringUtils.isEmpty(name)){
            b_calculate_codeList = b_calculate_codeRepository.findAll();
        }else{
            b_calculate_codeList = b_calculate_codeRepository.findByNameLike(name);
        }
        dataListVO.setRows(b_calculate_codeList);
        dataListVO.setTotal(b_calculate_codeList.size());
        return dataListVO;
    }

    @RequestMapping(value = "/caculateEnv/list", method = RequestMethod.GET)
    public DataListVO caculateEnvList(@RequestParam(value = "caculateId") Integer caculateId){
        DataListVO dataListVO = new DataListVO();
        List<B_Calculate_Env> b_calculate_envList = b_calculate_envRepository.findByCalculateCode(caculateId);
        dataListVO.setRows(b_calculate_envList);
        dataListVO.setTotal(b_calculate_envList.size());
        return dataListVO;
    }

    @RequestMapping(value = "/caculateEnv/properties", method = RequestMethod.GET)
    public DataListVO caculateEnvList(){
        DataListVO dataListVO = new DataListVO();
        B_Calculate_Code bCalculateCode = b_calculate_codeRepository.findByEnable(ValueConstant.ENABLE);
        if(bCalculateCode != null){
            List<B_Calculate_Env> b_calculate_envList = b_calculate_envRepository.findByCalculateCode(bCalculateCode.getId());
            List<Map<String, Object>> values = new ArrayList<>();
            for (B_Calculate_Env b_calculate_env : b_calculate_envList) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", b_calculate_env.getId());
                map.put("calculate_env_key", b_calculate_env.getCalculate_env_key());
                map.put("name", b_calculate_env.getCalculate_env_name());
                map.put("value", b_calculate_env.getCalculate_env_value());
                map.put("editor", "text");
                values.add(map);
            }
            dataListVO.setRows(values);
            dataListVO.setTotal(values.size());
        }else{
            throw new BusiException(HttpStatus.BAD_REQUEST.value(), "请先启用结算规则");
        }
        return dataListVO;
    }

    @RequestMapping(value = "/pod/hongbao", method = RequestMethod.GET)
    public DataListVO pod_hongbao(@RequestParam(value = "pod_id", required = false) Integer podId){
        DataListVO dataListVO = new DataListVO();
        Integer newPodId = mgPodId(podId);
        if(newPodId != null){
            List<B_HongBao> bHongBaoList = b_hongBaoRepository.findByPod_id(newPodId);
            dataListVO.setRows(bHongBaoList);
            dataListVO.setTotal(bHongBaoList.size());
        }
        return dataListVO;
    }

    @RequestMapping(value = "/pod/zhuangjia", method = RequestMethod.GET)
    public ShangZhuangVO pod_zhuangjia(@RequestParam(value = "pod_id", required = false) Integer podId){
        ShangZhuangVO shangZhuangVO = new ShangZhuangVO();
        Integer newPodId = mgPodId(podId);
        if(newPodId != null){
            B_Pod pod = b_podRepository.findOne(newPodId);
            shangZhuangVO.setZhuangName(pod.getZhuang_name());
            shangZhuangVO.setZhuangMoney(ValueUtil.formatMoney(pod.getZhuang_money()));
        }
        return shangZhuangVO;
    }

    @RequestMapping(value = "/message/settings", method = RequestMethod.GET)
    public MessageTemplateVO message_settings(){
        List<B_AutoMessage_Template> list = b_autoMessage_templateRepository.findAll();
        return BusiConventer.toMessageTemplateVO(list);
    }

    private Integer mgPodId(Integer podId){
        Integer newPodId = null;
        if(podId != null){
            newPodId = podId;
        }else{
            B_Pod currPod = busiService.getCurrPod();
            if(currPod == null){
                return null;
            }
            newPodId = currPod.getId();
        }
        return newPodId;
    }
}
